﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;

using System.Collections;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Reflection;
using System.Data;
using System.Windows.Forms;
namespace YNCNumberSys
{
    /// <summary>
    /// CAD测试类，不属于该项目，借用环境。
    /// </summary>
    public class CadHelper
    {
        public void FineText()
        {
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            sfd.Filter = "Excel(*.xls)|*.xls";//设置文件类型
            sfd.FileName = @"E:\CAD文字提取测试\20231205CAD的关键文字信息提取.xls";//设置默认文件名
            sfd.DefaultExt = "xls";//设置默认格式（可以不设）
            sfd.AddExtension = true;//设置自动在文件名中添加扩展名
            if (sfd.ShowDialog() == DialogResult.OK)
            {

            }
                
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("文字", typeof(string));
            

            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以读模式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                OpenMode.ForRead) as BlockTable;
                // 以读模式打开块表记录模型空间
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                OpenMode.ForRead) as BlockTableRecord;
                int nCnt = 0;
                acDoc.Editor.WriteMessage("\nModel space objects: ");
                string xx = string.Empty;
                // 遍历模型空间里的每个对象，并显示找到的对象的类型
                foreach (ObjectId acObjId in acBlkTblRec)
                {
                    // acEntity ent = (Entity)
                    DBObject t=acTrans.GetObject(acObjId, OpenMode.ForWrite);
                    if(acObjId.ObjectClass.DxfName=="TEXT")
                    {
                        DBText dbtext = (DBText)t;
                        if (dbtext != null)
                        {
                            string x=dbtext.TextString;
                            if (x.Contains(">") || x.Contains("<"))
                            {
                                acDoc.Editor.WriteMessage("\n" + x);
                                DataRow dr = dt.NewRow();
                                dr["文字"] = x; ;
                                dt.Rows.Add(dr);
                                xx = xx + "\n" + x;
                            }
                        }
                    }
                    
                    nCnt = nCnt + 1;
                }
                // 如果没发现对象则显示提示信息
                if (nCnt == 0)
                {
                    acDoc.Editor.WriteMessage("\n No objects found");
                }
                // 关闭事务
            }
            //Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            //Database db = doc.Database; Editor ed = doc.Editor;
            //Entity entity = null;
            //PromptEntityResult ent = ed.GetEntity(word);
            //if (ent.Status == PromptStatus.OK)
            //{
            //    using (Transaction transaction = db.TransactionManager.StartTransaction())
            //    {
            //        entity = (Entity)transaction.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
            //        transaction.Commit();
            //    }
            //}

            //// 创建选择集对象
            //MxDrawSelectionSet ss = new MxDrawSelectionSet();
            //// 创建过滤对象.
            //MxDrawResbuf spFilte = new MxDrawResbuf();
            //// 把文字对象，当着过滤条件.
            //spFilte.AddStringEx("TEXT,MTEXT", 5020);
            //// 得到图上，所有文字对象.
            //ss.Select2(MCAD_McSelect.mcSelectionSetAll, null, null, null, spFilte);
            //// 遍历每个文字.
            //bool bFind = false;
            //// 获得输入的文字
            //string str = textBox1.Text;
            //for (int i = 0; i < ss.Count; i++)
            //{
            //    MxDrawEntity ent = ss.Item(i);
            //    if (ent == null)
            //        continue;
            //    if (ent.ObjectName == "McDbText")
            //    {
            //        // 是一个文字
            //        MxDrawText text = (MxDrawText)ent;
            //        string sTxt = text.TextString;
            //        if (sTxt == str)
            //        {
            //            // 找到把文字放到视区中间.
            //            axMxDrawX1.PutEntityInView(ent.ObjectID, 300);
            //            var dLen = axMxDrawX1.ViewLongToDocCoord(80);
            //            // 绘制一个标记圆.
            //            axMxDrawX1.DrawVectorCircle(text.Position.x,
            //            text.Position.y,
            //            dLen, 65280);
            //            bFind = true;
            //        }
            //    }
            //    else if (ent.ObjectName == "McDbMText")
            //    {
            //        // 找到一个多行文字
            //        MxDrawResbuf param = new MxDrawResbuf();
            //        param.AddObjectId(ent.ObjectID);
            //        MxDrawMText mtext = (MxDrawMText)ent;
            //        // 得到多行文字中的，文字内容.
            //        MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.CallEx("Mx_GetMTextContent", param);
            //        if (ret.AtString(0) == "Ok")
            //        {
            //            if (ret.AtString(1) == str)
            //            {
            //                // 把文字放到视区.
            //                axMxDrawX1.PutEntityInView(ent.ObjectID, 300);
            //                var dLen = axMxDrawX1.ViewLongToDocCoord(80);
            //                // 绘制一个标记圆.
            //                axMxDrawX1.DrawVectorCircle(mtext.Location.x,
            //                    mtext.Location.y,
            //                    dLen, 65280);
            //                bFind = true;
            //                break;
            //            }
            //        }
            //    }
            //}
            //if (bFind)
            //    MessageBox.Show("找到文字");
            //else
            //    MessageBox.Show("没有找到文字");

        }
    }
}
